In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
In a fast developing firm, new employees are often hired. Each new employee is assigned a direct superior, whose superiors (both direct and indirect) become indirect superiors of .
We call the direct superior of a superior of degree 0. The superior of a superior of degree 0 has a degree equal to 1. In general, a superior of a superior of degree has degree . In this way, an employee is a subordinate of his immediate superior and superiors of higher degree. This defines a hierarchy of all employees, which has the founder of the company on its top.
The history of all employees who have joined the company since the foundation is recorded. Some employees wonder for how many subordinates they are superiors of degree . Would you mind writing a program, which will assist them in solving this problem, so that they could go back to work?
In the first line of the standard input there is an integer (), denoting the number of events. The following lines contain descriptions of the events, one per line, given in chronological order.
An event of hiring a new employee is described by a character 'Z' and two integers and (, for ), which represent the numbers of the new employee and his immediate superior, respectively. is equal to the number of some employee, who is currently hired. The founder is assigned number 1.
A question from an employee about the number of his subordinates, for whom he is a superior of degree , is described by a character 'P' and two integers and (, ).
Before the first event the founder was the only person working in the firm.
For each question from an employee output one line to the standard output with one integer equal to the number of subordinates of this employee, for whom he is a superior of degree .
For the input data:
8 Z 2 1 P 1 0 Z 3 1 Z 4 2 P 1 1 P 1 0 Z 5 2 P 2 0
the correct result is:
1 1 2 2
Task author: Jacek Tomasiewicz.